var clickFunction = function (keyEvent) {
    keyEvent = keyEvent ? keyEvent : window.event;
    keyEvent.which = keyEvent.which ? keyEvent.which : keyEvent.keyCode;
    keycontrol(keyEvent);
};
//window.document.onkeypress = clickFunction;
window.document.onkeydown = clickFunction;

//按键操作
function keycontrol (obj) {
    if (obj.which == 0x0026) {
        /* 上 */
        keyopt.moveBtn("go_top");
    } else if (obj.which == 0x0028) {
        /* 下 */
        keyopt.moveBtn("go_bottom");
    } else if (obj.which == 0x0025) {
        /* 左 */
        keyopt.moveBtn("go_left");
    } else if (obj.which == 0x0027) {
        /* 右 */
        keyopt.moveBtn("go_right");
    } else if (obj.which == 0x000d) {
        /* 确定 */
        keyopt.marksure();
    } else if (
        (obj.which <= 0x0039 && obj.which >= 0x0030) ||
        obj.which == 0x118 ||
        obj.which == 280
    ) {
        //数字键输入
        keyopt.input(obj.which);
    } else if (obj.which == 113 || obj.which == 0x0008) {
        obj.stopPropagation();
        obj.preventDefault();
        if (document.getElementById('video') && videowindow.getAttribute("width") == "1280") {
            $mp.setVideoDisplayMode(0);
            videowindow.setAttribute("width", "633");
            videowindow.setAttribute("height", "396");
            document.getElementById('video').style.top = '196px';
            document.getElementById('video').style.left = '320px';
            document.getElementById('video').style.zIndex == 'initial';
            videoPluginObj.setOutputVideoWindow(323, 199, 633, 396);
        } else {
            if (document.getElementById('video')){
                // $mp.stop();
                $mp.clearVideoOutput();
                $mp.unbindPlayerInstance(nativePlayerInstanceID);
            }
            history.go(-1);
        }
    } else if (obj.which == 0x0447) {//音量＋键
        volUp();
    } else if (obj.which == 0x0448) {//音量－键
        volDown();
    }
}

//操作类型
var keyopt = {
    options: {
        focusElm: "t1"
    },
    moveBtn: function (toDirection) {
        // 移动之前的id
        var oldBtnId = this.options.focusElm;
        // 将移动之前的元素json信息解析成json对象
        var old_obj_json = eval(oldBtnId + "_json");
        // 获取移动目的地的id信息
        var goElm = old_obj_json[toDirection];
        if (goElm == "") {
            goElm = oldBtnId;
        }
        var new_btn = document.getElementById(goElm);
        if (new_btn) {
            var onfocusFuc = eval(goElm + "_onfocus"); //之后ID的blur方法
            var onblurFuc = eval(oldBtnId + "_onblur"); //之前ID的focus方法
            onblurFuc(toDirection, oldBtnId); //go_buttom,top1
            this.options.focusElm = goElm;
            onfocusFuc(toDirection, goElm); //go_buttom,top2
        }
    },
    marksure: function () {
        var dom = document.getElementById(this.options.focusElm);
        if (dom) {
            var onsubmitFuc = eval(this.options.focusElm + "_onsubmit");
            if (dom.dataset.url) {
                location.href = dom.dataset.url;
            }
            onsubmitFuc("", this.options.focusElm);
        }
    },
    input: function (code) {
        var onkeypressFuc = eval(this.options.focusElm + "_onkeypress");
        onkeypressFuc(code);
    },
    moveToElm: function (id) {
        var to_elm = document.getElementById(id);
        if (to_elm) {
            var onfocusFuc = eval(id + "_onfocus");
            var onblurFuc = eval(this.options.focusElm + "_onblur");
            onblurFuc('',this.options.focusElm);
            this.options.focusElm = id;
            onfocusFuc('',this.options.focusElm);
        }
    },
    addClass: function (id, className) {
        document.getElementById(id).className += " " + className;
    },
    removeClass: function (id, className) {
        //top1,,,top
        document.getElementById(id).className = document
            .getElementById(id)
            .className //class="1"
            .replace(className, "");
    },
    replaceClass: function (id, searchClass, replaceClass) {
        var className = document.getElementById(id).className;
        document.getElementById(id).className = className.replace(
            searchClass,
            replaceClass
        );
    }
};


// function roll (selector, num) {
//     //获取焦点是滚动,传入选择器，以及最长字符长度
//     var val = document.querySelectorAll(selector)[0].innerHTML;
//     if (val.length > num) {
//         document.querySelectorAll(selector)[0].innerHTML = "<marquee style='vertical-align: top;'>" + val + "</marquee>";
//     }
// }
//
// function removeRoll (selector) {
//     //失去焦点是滚动,传入选择器
//     document.querySelectorAll(selector)[0].innerHTML = document
//         .querySelectorAll(selector)[0]
//         .innerHTML.replace('<marquee style="vertical-align: top;">', "")
//         .replace("</marquee>", "");
// }
//
//
// if (!Object.assign) {
//     Object.defineProperty(Object, "assign", {
//         enumerable: false,
//         configurable: true,
//         writable: true,
//         value: function (target, firstSource) {
//             "use strict";
//             if (target === undefined || target === null)
//                 throw new TypeError("Cannot convert first argument to object");
//             var to = Object(target);
//             for (var i = 1; i < arguments.length; i++) {
//                 var nextSource = arguments[i];
//                 if (nextSource === undefined || nextSource === null) continue;
//                 var keysArray = Object.keys(Object(nextSource));
//                 for (var nextIndex = 0, len = keysArray.length; nextIndex < len; nextIndex++) {
//                     var nextKey = keysArray[nextIndex];
//                     var desc = Object.getOwnPropertyDescriptor(nextSource, nextKey);
//                     if (desc !== undefined && desc.enumerable) to[nextKey] = nextSource[nextKey];
//                 }
//             }
//             return to;
//         }
//     });
// }
//
// // 文字内容翻页
// function TextRoll (options) {
//     this.default = {
//         dom: null,      //内容dom对象
//         page: 0,        //当前页数
//         pageH: 300,     //每页高度，与文字显示高度相同
//         prevBtn: null,  //上一页按钮dom
//         nextBtn: null   //下一页按钮dom
//     };
//     this.options = Object.assign(this.default, options);
//     var that = this;
//     this.options.dom.innerHTML = '<div style="position: absolute;top: 0;left: 0;">' + this.options.dom.innerHTML + '</div>';
//     this.options.dom = this.options.dom.getElementsByTagName('div')[0];
//     // this.options.dom.innerHTML = '<p>' + str.replace(/&lt;br\/&gt;/g, '</p><p>') + '</p>';
//     this.options.pageMax = Math.ceil(this.options.dom.offsetHeight / this.options.pageH);
//     this.setPage = function (num) { //翻页，num为0表示上一页，num为1表示是下一页
//         if (num == 0 && that.options.page != 0) {
//             that.options.page--;
//             that.options.dom.style.top = -(that.options.page) * that.options.pageH + 'px';
//         } else if (num == 1 && that.options.page < that.options.pageMax - 1) {
//             that.options.page++;
//             that.options.dom.style.top = -(that.options.page) * that.options.pageH + 'px';
//         }
//         setPageBtn();
//     };
//     // 控制翻页按钮显示隐藏
//     var setPageBtn = function () {
//         if (that.options.prevBtn) {
//             if (that.options.page == 0) {
//                 that.options.prevBtn.style.display = 'none';
//             } else {
//                 that.options.prevBtn.style.display = 'block';
//             }
//         }
//         if (that.options.nextBtn) {
//             if (that.options.page == that.options.pageMax - 1) {
//                 that.options.nextBtn.style.display = 'none';
//             } else {
//                 that.options.nextBtn.style.display = 'block';
//             }
//         }
//     }
//     this.prev = function () {
//         this.setPage(0);
//     };
//     this.next = function () {
//         this.setPage(1);
//     };
// }
